﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.Console
{
    public static class Index
    {
        public static Adams.Admin.DataTransfer.Console.Index.PageLoad PageLoad(int userId)
        {
            var data = new Adams.Admin.DataTransfer.Console.Index.PageLoad();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT A.*
FROM Console.Applications A
INNER JOIN Console.UsersApplications UA ON UA.ApplicationID = A.ApplicationID
WHERE UA.UserID = @UserID

SELECT COUNT(*) AS Count
FROM Classifieds.Postings
WHERE StatusID = 1

SELECT COUNT(*) AS Count
FROM Businesses.Businesses
WHERE StatusID = 1
", CommandType.Text,
                   DB.CreateParameter("@UserID", userId, SqlDbType.Int)))
            {
                data.Applications = new Adams.Shared.Entities.Console.ApplicationCollection();
                while (dr.Read())
                {
                    var app = new Adams.Shared.Entities.Console.Application();
                    app.ID = dr["ApplicationID"].DbInt();
                    app.Name = dr["Name"].DbString();
                    app.Controller = dr["Controller"].DbString();
                    app.Url = dr["Url"].DbString();
                    app.Icon = dr["Icon"].DbString();
                    app.DisplayOrder = dr["DisplayOrder"].DbInt();
                    data.Applications.Add(app);
                }

                dr.NextResult();
                dr.Read();
                data.PendingClassifiedPostings = dr["Count"].DbInt();

                dr.NextResult();
                dr.Read();
                data.PendingBusinesses = dr["Count"].DbInt();
            }

            return data;
        }
    }
}
